﻿using System.Collections.Generic;
using System.Linq;

namespace ComeSeeThis_MVC2.Models
{
    public class MotorRepository
    {
        private readonly ComeSeeThisDBEntities _entities = new ComeSeeThisDBEntities();

        #region Query Methods

        #region Motors
        /// <summary>
        /// Gets a list of all motors
        /// </summary>
        /// <returns>An IQueryable list of motors</returns>
        /// <param name="approved">If set to true, only returns Approved motors, otherwise only returns unapproved motors</param>
        /// <param name="includeAny">If set to 0, includes the * Any Motor record, otherwise does not</param>
        public IQueryable<Motor> GetAllMotors(int includeAny=1, bool approved=true)
        {
            return _entities.Motors.Where(x=>x.Approved==approved).Where(x=>x.MotorId > includeAny).OrderBy(m => m.MotorName).OrderBy(m => m.Company.CompanyName);
        }

        public IQueryable<Motor> GetAllMotorsByCompanyId(int companyId)
        {
            return _entities.Motors.Where(c => c.CompanyId == companyId);
        }

        public Motor GetMotor(int motorId)
        {
            return _entities.Motors.SingleOrDefault(c => c.MotorId == motorId);
        }

        public IQueryable<Motor> GetMotorsThatHaveFdrs()
        {
            return _entities.Motors.Where(g => g.GearingRecs.Any());
        }

        public IQueryable<Motor> GetMotorsThatHaveFdrs(int vehicleTypeId)
        {
            var gearingRecs = new GearingRecRepository().GetAllGearingRecsByVehicleTypeId(vehicleTypeId);
            return gearingRecs.Select(gearingRec => gearingRec.Motor);

            //return _entities.Motors.Where(g => new GearingRecRepository().GetAllGearingRecs().Where(vt => vt.VehicleTypeId == vehicleTypeId).Any());
        }

        public IQueryable<Motor> GetMotorsThatHaveFdrs(int vehicleTypeId, int vehicleId)
        {
            //var gearingRecs = new GearingRecRepository().GetAllGearingRecsByVehicleTypeId(vehicleTypeId);
            //return (from gearingRec in gearingRecs where gearingRec.VehicleId == vehicleId select gearingRec.Motor);

            return _entities.Motors.Where(g => new GearingRecRepository().GetAllGearingRecs().Where(v => v.VehicleId == vehicleId).Any());
        }
        #endregion

        #region Motor Sizes

        public IQueryable<MotorSize> GetAllMotorSizes()
        {
            return _entities.MotorSizes.OrderBy(m => m.MotorSizeName);
        }

        public MotorSize GetMotorSize(int motorSizeId)
        {
            return _entities.MotorSizes.SingleOrDefault(c => c.MotorSizeId == motorSizeId);
        }

        #endregion

        #endregion

        #region Insert / Delete Methods

        public void Add(Motor motor)
        {
            _entities.Motors.AddObject(motor);
        }

        public void Delete(Motor motor)
        {
            // Delete the motor
            _entities.Motors.DeleteObject(motor);
        }

        #endregion

        #region Persistance

        public void Save()
        {
            _entities.SaveChanges();
        }

        #endregion
    }
}